package com.wm.blog_admin.service;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wm.blog_admin.dao.TAdminRoleDAO;
import com.wm.blog_admin.query.TAdminRoleQuery;
import com.wm.blog_common.base.BeanUtils;
import com.wm.blog_common.bean.PageHelper;
import com.wm.blog_common.bean.QueryPage;
import com.wm.blog_common.domain.TAdminRoleDO;
import com.wm.blog_common.entity.TAdminRole;
import com.wm.blog_common.result.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author wm_yu
 * @since 2020-03-24
 */
@Service
public class TAdminRoleService{
    @Autowired
    private TAdminRoleDAO tAdminRoleDAO;

    /**
     * 根据ID查询
     * @param id
     * @return
     */
    public Result<TAdminRoleDO> selectById(Long id){
        return Result.success(BeanUtils.copyProperties(tAdminRoleDAO.getById(id),TAdminRoleDO.class));
    }

    /**
    * 根据ID删除
    * @param id
    * @return
    */
    public Result deleteById(Long id){
        return tAdminRoleDAO.removeById(id)?Result.success():Result.failure();
    }

    /**
    * 添加
    * @param tAdminRole
    * @return
    */
    public Result insert(TAdminRole tAdminRole){
        return tAdminRoleDAO.save(tAdminRole)?Result.success():Result.failure();
    }

    /**
    * 修改
    * @param tAdminRole
    * @return
    */
    public Result update(TAdminRole tAdminRole){
        return tAdminRoleDAO.updateById(tAdminRole)?Result.success():Result.failure();
    }

    /**
    * 统计总数
    *
    * @param query
    * @return
    */
    public Result<Integer> count(TAdminRoleQuery query) {
        return Result.success(tAdminRoleDAO.count(this.combineQuery(query)));
    }

    /**
    * 列表查询
    * @param query
    * @return
    */
    public Result<List<TAdminRoleDO>> list(TAdminRoleQuery query){
        return Result.success(BeanUtils.copyListProperties(tAdminRoleDAO.list(this.combineQuery(query)),TAdminRoleDO.class));
    }
    /**
    * 分页查询
    * @param page
    * @return
    */
    public Result<Page<TAdminRoleDO>> page(QueryPage page, TAdminRoleQuery query){
        return Result.success(PageHelper.newPage(tAdminRoleDAO.page(page,this.combineQuery(query)),TAdminRoleDO.class));
    }

    /**
    * 组装查询条件
    *
    * @param query
    * @return
    */
    private QueryWrapper<TAdminRole> combineQuery(TAdminRoleQuery query) {
        QueryWrapper<TAdminRole> wrapper = new QueryWrapper<>();
        //todo 查询条件 wrapper.eq("字段名","值")
        return wrapper;
    }

}







